import 'package:flutter/material.dart';

import '../../utils/common.dart';
import 'dialog_bottom_anim.dart';
import 'dialog_center_anim.dart';


enum DefaultAnim { Bottom, Center }

class DialogRouter<T> extends PageRouteBuilder<T> {
  final Widget page;

  DialogRouter(
    this.page, {
    Color? backgroundColor,
    clickExternalClose = true,
    clickClose = false,
    RouteTransitionsBuilder? transitionsBuilder,
    DefaultAnim defaultAnim = DefaultAnim.Bottom,
  }) : super(
          opaque: false,
          pageBuilder: (context, animation, secondaryAnimation) => page,
          transitionsBuilder: (BuildContext context,
                  Animation<double> animation1,
                  Animation<double> animation2,
                  Widget child) =>
              _transitionsBuilder(
                  context,
                  animation1,
                  animation2,
                  _defaultAttr(
                    context,
                    child,
                    clickExternalClose,
                    clickClose,
                  ),
                  backgroundColor,
                  transitionsBuilder,
                  defaultAnim),
        );

  /// 跳转配置
  static _transitionsBuilder(
    BuildContext context,
    Animation<double> animation1,
    Animation<double> animation2,
    Widget child,
    Color? backgroundColor,
    RouteTransitionsBuilder? transitionsBuilder,
    DefaultAnim defaultAnim,
  ) {
    return Stack(
      children: <Widget>[
        GestureDetector(
          child: AnimatedBuilder(
            builder: (_, child) => Opacity(
              opacity: animation1.value,
              child: child,
            ),
            child: DecoratedBox(
              decoration: BoxDecoration(
                  color: backgroundColor ?? Colors.black.withOpacity(0.55)),
              child: const SizedBox.expand(),
            ),
            animation: animation1,
          ),
        ),
        transitionsBuilder?.call(context, animation1, animation2, child) ??
            _defaultAnim(
              animation1,
              child,
              defaultAnim,
            ),
      ],
    );
  }

  /// 处理样式/交互
  static _defaultAttr(
    BuildContext context,
    Widget child,
    bool clickExternalClose,
    bool clickClose,
  ) {
    return child = debounceClickBtn(
      onTap: () {
        if (clickExternalClose) {
          if (Navigator.canPop(context)) {
            Navigator.pop(context);
          }
        }
      },
      child: Material(
        color: Colors.transparent,
        child: GestureDetector(
          onTap: () {
            if (clickClose) {
              if (Navigator.canPop(context)) {
                Navigator.pop(context);
              }
            }
          },
          child: Padding(
            padding: EdgeInsets.only(
              bottom: MediaQuery.of(context).viewInsets.bottom,
            ),
            child: child,
          ),
        ),
      ),
    );
  }

  /// 默认动画
  static _defaultAnim(
      Animation<double> animation1, Widget child, DefaultAnim defaultAnim) {
    switch (defaultAnim) {
      case DefaultAnim.Bottom:
        return DialogBottomAnim(
          animation: animation1,
          child: child,
        );
      case DefaultAnim.Center:
        return DialogCenterAnim(
          animation: animation1,
          child: child,
        );
    }
  }
}
